﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;

using static System.Net.Mime.MediaTypeNames;

namespace SuperKit
{
    /*
     * 这个项目是用来把不需要的字符串从项目中移除。以前的移除功能太泛用了，每次都需要配置。
     * 这个一口气就处理了，不需要配置。以后有什么需要移除的符号，直接加在这里就行。
     */
    class SafeRename : CommandExecutor {
        private class RemovePair {
            public string Text { get; }
            public string Replace { get; }

            public RemovePair(string text, string replace = Kit.EMPTY) {
                Text = text;
                Replace = replace;
            }
        }

        private class RegPair {
            public Regex reg { get; }
            public string Replace { get; }

            public RegPair(string text, string replace = Kit.EMPTY) {
                reg = new Regex(text);
                Replace = replace;
            }
        }

        private List<RemovePair> PAIRS;
        private List<RegPair> REG_PAIRS;
        private int nameChanged;


        public SafeRename(ArgReader arg, IExecutorOwner form, string fpath) : base(arg, form, fpath) {
            PAIRS = new List<RemovePair>();
            REG_PAIRS = new List<RegPair>();

            Add("zip删除", "zip");
            Add("7删z", "7z");

            Add(new string[] { "(同人CG集)", "[DL版]", "(同人誌)", "(成年コミック)" });

            AddReg(new string[] {
                @"\(C\d\d\)\s*",
                @"\(COMIC\d\S\d\)\s*",
                @"\(サンクリ\w\w\)\s*",
                @"\(ふたけっと\s*\w+\)\s*",
                @"\(サンクリ\w+\)\s*",
                @"\(紅楼夢\w+\)\s*",
                @"\(例大祭\w+\)\s*",
            });        
        }

        private void Add(string[] strings) {
            foreach (var s in strings) {
                PAIRS.Add(new RemovePair(s, Kit.EMPTY));
            }
        }

        private void Add(string text, string replace = Kit.EMPTY) {
            PAIRS.Add(new RemovePair(text, replace));
        }

        private void AddReg(string text, string replace = Kit.EMPTY) {
            REG_PAIRS.Add(new RegPair(text, replace));
        }

        private void AddReg(string[] texts) {
            foreach (var s in texts) {
                REG_PAIRS.Add(new RegPair(s, Kit.EMPTY));
            }
            
        }

        public override void Execute() {
            base.Execute();

            DirectoryInfo info = new DirectoryInfo(FilePath);
            var files = Kit.GetAllFiles(null, info);

            nameChanged = 0;

            foreach (var i in files) {
                Process(i);
            }

            var dirs = Kit.GetAllDirectories(null, info);

            foreach (var i in dirs) {
                ProcessDir(i);
            }

            End();
        }

        private void ProcessDir(DirectoryInfo dir) {
            var name = ReplaceName(dir.Name);
            var path = Path.Combine(dir.Parent.FullName, name);

            if (Kit.IsEqual(path, dir.FullName)) {
                return;
            }

            nameChanged++;

            if (!Directory.Exists(path)) {
                Directory.Move(dir.FullName, path);
                SetStatus($"改名目录({nameChanged}+): {dir.Name} => {name}");
            }
        }

        private void Process(FileInfo f) {
            var name = ReplaceName(f.Name);
            var path = Path.Combine(f.Directory.FullName, name);

            if (Kit.IsEqual(path, f.FullName)) {
                return;
            }

            nameChanged++;

            // 常规检查（如果文件小于3个字符，则表示有地方出问题了，跳过)
            if (path.Length < 3) {
                Flush($"将{f.Name}改名时字符数小于3，跳过修改。");
                return;
            }

            if (!File.Exists(path)) {
                File.Move(f.FullName, path);
                SetStatus($"改名({nameChanged}+): {f.Name} => {name}");
            }
        }

        private string ReplaceName(string name) {
            foreach (var i in PAIRS) {
                name = name.Replace(i.Text, i.Replace);
            }

            foreach (var i in REG_PAIRS) {
                name = i.reg.Replace(name, i.Replace);
            }

            name = name.Replace("  ", " ");

            return name.Trim();
        }
    }
}
